package ModelLayer;

import java.util.*;
public class DVDCon
{
    private ArrayList<DVD> dvds;
    private static DVDCon instance;//Singleton
    
    /**
    * Constructor for objects of class DVDCon
    */
    private DVDCon()
    {
        dvds = new ArrayList<DVD>();
    }
    
    /**
     * Singleton method for working with only one instance of DVDCon
     */
    public static DVDCon getInstance()
    {
       if(instance == null)
           {
               instance = new DVDCon();
           }
       return instance;
    }
    
    /**
     * Creates DVD's and adding them to the DVDContainer.
     */
    public void addDVD(String newTitle, String newArtist, String newReleaseDate)
    {
        DVD newDVD = new DVD (newTitle, newArtist, newReleaseDate);
        dvds.add(newDVD);
    }
    
    /**
     * Creates copies and adds them to an instance of dvd.
     */
    public void addCopy(long newId, String newPurchaseDate, int newPurchasePrice)
    {
        getDVD(newId).setCopy(newPurchaseDate, newPurchasePrice);
    }
    
    /**
     * prints a list of all dvds in the dvd container
     */
    public void getDVDCon()
    {
        for (DVD dvd : dvds)
        {
             System.out.println("id: " + dvd.getId() + "\t title" + dvd.getTitle());
        }
    }
    
    /**
     * Find a dvd from it's ID in the DVDContainer.
     */
    public DVD getDVD(long newId)
    {
        int index = 0;
        boolean found = false;
        
        while(index < dvds.size() && !found)
        {
            DVD tempDVD = dvds.get(index);
            if(tempDVD.getId() == newId)
            {
                found = true;
            }
            
            else {
                index++;
            }
        }
        
        if(found)
        { 
            return dvds.get(index);
        }
        
        else {
            return null;
        }
    }
    
    
    /**
     * Updates a dvd from it's ID in the DVDContainer.
     */
    public void updateDVD(DVD d, String newTitle, String newArtist, String newReleaseDate)
    {
     d.updateDVD( newTitle, newArtist, newReleaseDate);
    }
    
    /**
     * Removes a copy from it's ID in the DVDContainer.
     */
    public void deleteCopy(long newId, long newSerialNr)
    {
        getDVD(newId).deleteCopy(newSerialNr);
    }
    
    /**
     * Removes a dvd from it's ID in the DVDContainer.
     */
    public void deleteDVD(long id)
    {
        dvds.remove(getDVD(id));
    }
    
    /**
     * Returns an ArrayList of all copies.
     */
    public ArrayList<Copy> getAllCopies()
    {
        return instance.getInstance().getAllCopies();
    }
    
}